#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <ev.h>

#define SERVER_PORT 6015
#define CLIENT_IP "192.168.35.212" // 客户端 IP，替换为实际值
#define CLIENT_IF_NAME "enp2s0"
#define CLIENT_PORT 6014
#define BUFFER_SIZE 1024

struct ServerContext {
    int sockfd;
    struct sockaddr_in client_addr;
    ev_io io_watcher;
    ev_timer timer_watcher;
};
ServerContext ctx;

// 接收数据的回调函数
static void recv_cb(EV_P_ ev_io *w, int revents) {
    ServerContext *ctx = static_cast<ServerContext*>(w->data);
    char buffer[BUFFER_SIZE];
    struct sockaddr_in from_addr;
    socklen_t addr_len = sizeof(from_addr);

    ssize_t len = recvfrom(ctx->sockfd, buffer, BUFFER_SIZE, 0,
                           (struct sockaddr*)&from_addr, &addr_len);
    if (len < 0) {
        if (errno == EAGAIN || errno == EWOULDBLOCK) return; // 暂无数据
        perror("recvfrom failed");
        ev_break(EV_A_ EVBREAK_ALL);
        return;
    }
    buffer[len] = '\0';
    std::cout << "Received from client: " << buffer << std::endl;
}

// 发送数据的回调函数（定时器触发）
static void send_cb(EV_P_ ev_timer *w, int revents) {
    ServerContext *ctx = static_cast<ServerContext*>(w->data);
    std::string message = "Hello from server at " + std::to_string(time(nullptr));
    ssize_t sent = sendto(ctx->sockfd, message.c_str(), message.size(), 0,
                          (struct sockaddr*)&ctx->client_addr, sizeof(ctx->client_addr));
    if (sent < 0) {
        perror("sendto failed");
        ev_break(EV_A_ EVBREAK_ALL);
        return;
    }
    std::cout << "Sent to client: " << message << std::endl;
}
static int open_socket()
{
    // 创建 UDP socket
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        perror("socket creation failed");
        return 1;
    }

    // 设置 socket 为非阻塞
    int flags = fcntl(sockfd, F_GETFL, 0);
    fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

    // 绑定到本地 enp2s0 网卡
    struct sockaddr_in local_addr;
    memset(&local_addr, 0, sizeof(local_addr));
    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.s_addr = INADDR_ANY; // 绑定任意 IP
    local_addr.sin_port = htons(SERVER_PORT);

    struct ifreq ifr;
    strncpy(ifr.ifr_name, CLIENT_IF_NAME, IFNAMSIZ - 1);
    if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&ifr, sizeof(ifr)) < 0)
    {
        perror("SO_BINDTODEVICE failed");
        close(sockfd);
        return 1;
    }

    if (bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0)
    {
        perror("bind failed");
        close(sockfd);
        return 1;
    }

    // 设置客户端地址
    struct sockaddr_in client_addr;
    memset(&client_addr, 0, sizeof(client_addr));
    client_addr.sin_family = AF_INET;
    client_addr.sin_port = htons(CLIENT_PORT);
    if (inet_pton(AF_INET, CLIENT_IP, &client_addr.sin_addr) <= 0)
    {
        perror("inet_pton failed");
        close(sockfd);
        return 1;
    }
    ctx.sockfd = sockfd;
    ctx.client_addr = client_addr;
    return 0;
}

int main() {
   
    if(open_socket() != 0)
    {
        printf("error! open_socket!\n");
        exit(1);
    }
    // 初始化 libev 事件循环
    struct ev_loop *loop = EV_DEFAULT;
    // 设置接收数据的 watcher
    ev_io_init(&ctx.io_watcher, recv_cb, ctx.sockfd, EV_READ);
    ctx.io_watcher.data = &ctx;
    ev_io_start(loop, &ctx.io_watcher);

    // 设置定时器，每 100ms 发送一次
    ev_timer_init(&ctx.timer_watcher, send_cb, 0.1, 0.1); // 初次延迟 100ms，之后每 100ms
    ctx.timer_watcher.data = &ctx;
    ev_timer_start(loop, &ctx.timer_watcher);

    std::cout << "Server started, bound to enp2s0, sending to " << CLIENT_IP << ":" << CLIENT_PORT << std::endl;

    // 运行事件循环
    ev_run(loop, 0);

    // 清理
    close(ctx.sockfd);
    return 0;
}